<?php

namespace Cpadmin\Model;

use Zend\Db\TableGateway\AbstractTableGateway;
use Zend\Db\Adapter\Adapter;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\Sql\Sql;
use Cpadmin\Model\Admin;
use Zend\Session\Container;

class AdminTable extends AbstractTableGateway {

    protected $table = "tbladmin";
    protected $sql;

    public function __construct(Adapter $adapter) {
        $this->adapter = $adapter;
        $this->resultSetPrototype = new ResultSet();
        $this->resultSetPrototype->setArrayObjectPrototype(new Admin());
        $this->initialize();
        $this->sql = new Sql($this->adapter);
    }

    public function checkLogin(Admin $admin) {
        $user = $admin->getAdminUsername();
        $password = $admin->getAdminPassword();
        $sqlEx = $this->sql->select();
        $sqlEx->from($this->table);
        $sqlEx->columns(array("adminUsername" => "adminUsername", "adminpassword" => "adminpassword"));
        $sqlEx->where(array("adminUsername" => $user));
        $pst = $this->sql->prepareStatementForSqlObject($sqlEx);
        $resultSet = $pst->execute();
        $checkData = $resultSet->current();
        if ($checkData != null) {
            $code = $checkData["adminpassword"];
            $checkpass = $admin->getPassword($password, $code);
            if ($checkpass != null) {
                $session = new Container("ssadmin");
                $session->adminusername = $checkData["adminUsername"];
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
        return false;
    }

    public function checkpass($password) {
        $admin = new Admin();
        $sqlEx = $this->sql->select();
        $sqlEx->from($this->table);
        $session = new Container("ssadmin");
        $username = $session->adminusername;
        $sqlEx->columns(array("adminUsername" => "adminUsername", "adminpassword" => "adminpassword"));
        $sqlEx->where(array("adminUsername" => $username));
        $pst = $this->sql->prepareStatementForSqlObject($sqlEx);
        $resultSet = $pst->execute();
        $checkData = $resultSet->current();
        $code = $checkData["adminpassword"];
        $checkpass = $admin->getPassword($password, $code);
        if ($checkpass != null) {
            return true;
        } else {
            return false;
        }
    }
    public function changepasstable(Admin $admin){
        $session = new Container("ssadmin");
        $username = $session->adminusername;
        $passwordnew=$admin->getAdminPassword();
        $sqlEx=$this->sql->update();
        $sqlEx->table($this->table);
        $sqlEx->set(array("adminpassword"=>$admin->setPassword($passwordnew)));
        $sqlEx->where(array("adminUsername"=>$username));
        $pst=$this->sql->prepareStatementForSqlObject($sqlEx);
        $result=$pst->execute();
        if($result !=null){
            return true;
        }else{
            return false;
        }
    }

}
